package com.service.dwm;

import com.dao.BaseDao;
import com.entity.CatPost;
import com.entity.PageManager;
import com.entity.Varietiess;

import java.util.List;

public class CatPostService extends BaseDao {

    /*public Object[] getObjArr(User user){
        Object[] params = {user.getUsername(), user.getPassword(), user.getName(),
        user.getSex(), user.getAge(), user.getBirthday(), user.getPhone(), user.getHeight(),
        user.getPic()};
        return params;
    }*/

    /**
     * 增加用户数据
     * @param catPost
     * @return
     */
    public boolean addcatpost(CatPost catPost) {
        /*String sql = "INSERT INTO `rz1217`.`user`(`username`, `password`, " +
                "`name`, `sex`, `age`, `birthday`, `phone`, " +
                "`height`, `pic`) " +
                "VALUES (?,?,?,?,?,?,?,?,?)";
        Object[] params = getObjArr(user);*/
        try {
            return insert(catPost) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 分页查询
     * @param pm
     * @return
     */
    public List<CatPost> queryCatPosts(PageManager pm) {
        int start = (pm.getPn() - 1) * pm.getRn();
        int end = pm.getRn();
        String sql = "select * from catpost where state ='T' limit ?,?";
        return findList(CatPost.class, sql, start, end);
    }

    /**
     * 分页查询
     * @return
     */
    public List<CatPost> queryCatPosts() {
        String sql = "select * from catpost";
        return findList(CatPost.class, sql);
    }
    public List<CatPost> queryCatPostsCatbreed1() {
        String sql = "select * from catpost where catbreed = '布偶猫' and state ='T'";
        return findList(CatPost.class, sql);
    }
    public List<CatPost> queryCatPostsCatbreed2() {
        String sql = "select * from catpost where catbreed = '苏格兰折耳猫' and state ='T'";
        return findList(CatPost.class, sql);
    }
    public List<CatPost> queryCatPostsCatbreed3() {
        String sql = "select * from catpost where catbreed = '英国短毛猫' and state ='T'";
        return findList(CatPost.class, sql);
    }
    public List<CatPost> queryCatPostsCatbreed4() {
        String sql = "select * from catpost where catbreed = '孟买猫' and state ='T'";
        return findList(CatPost.class, sql);
    }
    public List<CatPost> queryCatPostsCatbreed5() {
        String sql = "select * from catpost where catbreed = '暹罗猫' and state ='T'";
        return findList(CatPost.class, sql);
    }
    public List<CatPost> queryCatPostsCatbreed6() {
        String sql = "select * from catpost where catbreed = '银渐层' and state ='T'";
        return findList(CatPost.class, sql);
    }
    public List<CatPost> queryCatPostsCatbreed7() {
        String sql = "select * from catpost where catbreed = '孟加拉豹猫' and state ='T'";
        return findList(CatPost.class, sql);
    }


    public CatPost queryCatPost(int id) {
//        String sql = "select * from user where id = ?";
        try {
            return (CatPost) query(new CatPost(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public int queryCatPostTotalCountT() {
        String sql = "select count(*) from catpost where state ='T'";
        return count(sql);
    }
    public int queryCatPostTotalCountT(String catpost) {
        String sql = "select count(*) from catpost where state ='T' and catbreed=?";
        return count(sql,catpost);
    }
    public int queryCatPostTotalCountF() {
        String sql = "select count(*) from catpost where state ='F'";
        return count(sql);
    }

    public boolean delCatPost(int id) {
        boolean result = false;
        try {
            result = deleteCatPost(new CatPost(id))> 0;
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean updateCatPost(int id) {
        String sql = "UPDATE catpost SET state = 'T' WHERE `id` = ?";
        return update(sql,id)>0;
    }

    public List<CatPost> queryCatPostsF(PageManager pm) {
        int start = (pm.getPn() - 1) * pm.getRn();
        int end = pm.getRn();
        String sql = "select * from catpost where state ='F' limit ?,?";
        return findList(CatPost.class, sql, start, end);
    }
}